package com.wptk.filemanager.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;


import com.wptk.filemanager.App;
import com.wptk.filemanager.ad.AdLoadManager;
import com.wptk.filemanager.model.Constant;
import com.wptk.filemanager.model.FileDataManager;
import com.wptk.filemanager.ui.SplashADActivity;
import com.wptk.filemanager.ui.SplashActivity;
import com.hz.sdka.ada.Splasha;

import java.util.ArrayList;
import java.util.List;


public class ActivityUtils {
    private static final String TAG = "ActivityUtils";
    public static int activityNum = 0;
    public static boolean isOut = true;

    public static void registerActivityLifecycle(Application context) {
        ManagerActivityUtils.INSTANCE.addTask("appTask", new ManagerActivityUtils.ActivityLifeCallback() {
            @Override
            public void onCreated(@Nullable Activity activity, @Nullable Bundle savedInstanceState) {
                ActivityUtils.addActivity(activity);
                if (activity.getClass().getSimpleName().equals(SplashActivity.class.getSimpleName())
                        || activity.getClass().getSimpleName().equals(SplashADActivity.class.getSimpleName())) {

                    App.spUtils.putLong(Constant.APP_QIHUAN_JGSJ, System.currentTimeMillis());
                }
            }

            @Override
            public void onStarted(@Nullable Activity activity) {

                activityNum++;
                LogUtils.e(TAG, "onActivityStarted  " + activity.getClass() + " activityNum =  " + activityNum);

                if (activityNum > 0) {

                    //Log.e(TAG, "从后台到前台 " + HZAdLoadSDK.adLoadShow);
                    if (isOut /*&& !HZAdLoadSDK.adLoadShow*/
                            && !activity.getClass().getSimpleName().equals(SplashActivity.class.getSimpleName())
                            && !activity.getClass().getSimpleName().equals(SplashADActivity.class.getSimpleName())) {

                        int adInterval = FileDataManager.appConfig.getFile_Config().getJgsj() >0
                                ? FileDataManager.appConfig.getFile_Config().getJgsj()
                                : 3;
                        Long time = App.spUtils.getLong(Constant.APP_QIHUAN_JGSJ, 0L);
                        Log.d(TAG,"adIntervalTime = " + adInterval);

                        if (adInterval != 0 && System.currentTimeMillis() - time >= adInterval * 1000) {

                            Log.d(TAG,"start splash ad.");
                            activity.startActivity(new Intent(activity, SplashADActivity.class));
                        }
                    }
                    isOut = false;
                }
            }

            @Override
            public void onResumed(@Nullable Activity activity) {

                LogUtils.e(TAG, "onActivityResumed  ");
            }

            @Override
            public void onPaused(@Nullable Activity activity) {
                LogUtils.e(TAG, "onActivityPaused  ");
            }

            @Override
            public void onStopped(@Nullable Activity activity) {

                activityNum--;
                LogUtils.e(TAG, "onActivityStopped  " + activity.getClass() + " activityNum =  " + activityNum);
                if (activityNum == 0) {

                    isOut = true;
                    Log.e(TAG, "前台回到了后台");
                    //AppActionManager.getInstance().startOutTimer();
                    App.spUtils.putLong(Constant.APP_QIHUAN_JGSJ, System.currentTimeMillis());

                    if (!Splasha.getInstance().isReady()){
                        AdLoadManager.INSTANCE.loadSplash();
                    }
                }
            }

            @Override
            public void onDestroyed(@Nullable Activity activity) {
                //if (activity.getClass().getSimpleName().equals(StartActivity.class.getSimpleName()))
                 //   return;
                ActivityUtils.removeActivity(activity);
            }
        });
        ManagerActivityUtils.INSTANCE.registerActivityListener(context);
    }


    private static final List<Activity> mActivities = new ArrayList<>();

    /**
     * 添加Activity
     *
     * @param activity activity
     */
    public static void addActivity(Activity activity) {
        synchronized (mActivities) {
            mActivities.add(activity);
        }
    }

    /**
     * 移除activity
     *
     * @param activity activity
     */
    public static void removeActivity(Activity activity) {
        synchronized (mActivities) {
            mActivities.remove(activity);
        }
    }


    /**
     * 关闭单个Activity
     *
     * @param clazz clazz
     */
    public static void finishActivity(Class<? extends Activity> clazz) {
        if (clazz == null) {
            return;
        }
        Activity act = null;
        for (Activity activity : mActivities) {
            if (activity.getClass().equals(clazz)) {
                act = activity;
            }
        }
        if (act != null) {
            act.finish();
            mActivities.remove(act);
        }
    }

    public static boolean hasActivity(Class<? extends Activity> clazz) {
        if (clazz == null) {
            return false;
        }
        for (Activity activity : mActivities) {
            if (activity.getClass().equals(clazz)) {
                return true;
            }
        }
        return false;
    }


    public static void moveActivityToBack(Class<? extends Activity> clazz) {
        if (clazz == null) {
            return;
        }
        for (Activity activity : mActivities) {
            if (activity.getClass().equals(clazz)) {
                activity.moveTaskToBack(true);
            }
        }
    }

    public static void moveAllActivityToBack() {
        if (mActivities.size() > 0) {
            for (Activity activity : mActivities) {
                activity.moveTaskToBack(true);
            }
        }
    }


    public static void moveActivityToFront(Class<? extends Activity> clazz) {

        if (clazz == null) {
            return;
        }
        for (Activity activity : mActivities) {
            if (activity.getClass().equals(clazz)) {
                Intent intent2 = new Intent(activity.getApplication(), activity.getClass());
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                activity.getApplication().startActivity(intent2);
            }
        }
    }

    /**
     * 关闭所有activity
     */
    public static void finishAllActivity() {
        try {
            LogUtils.d("finish", mActivities.size() + " ");
            if (mActivities.size() > 0) {
                for (Activity activity : mActivities) {
                    LogUtils.d("finish", activity.getClass().getSimpleName());
                    activity.finish();
                }
                mActivities.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getActivitySize() {
        return mActivities.size();
    }
}
